import namespace from "../namespace";
// import extend from "lodash/extend";

export class Line extends namespace.Figure {
  NAME = `${namespace.namespace}.shape.Line`;

  constructor(x1, y1, x2, y2) {
    super();

    this.start = new namespace.geo.Point(x1, y1);
    this.end = new namespace.geo.Point(x2, y2);

    return this;
  }

  createShapeElement() {
    let line = this.canvas.paper.path(
      "M" +
      this.start.x +
      " " +
      this.start.y +
      "L" +
      this.end.x +
      " " +
      this.end.y
    );
    return line;
  }

  setStartPoint(x, y) {
    if (this.start.equals({ x, y })) {
      return this;
    }

    this.start.setPosition(x, y);
    this.repaint();
    return this;
  }

  setEndPoint(x, y) {
    if (this.end.equals({ x, y })) {
      return this;
    }

    this.end.setPosition(x, y);
    this.repaint();

    return this;
  }

  repaint(attributes) {
    if (this.shape === null) {
      return;
    }

    if (typeof attributes === "undefined") {
      attributes = {
        path: [
          "M",
          this.start.x,
          this.start.y,
          "L",
          this.end.x,
          this.end.y
        ].join(" ")
      };
    } else {
      if (typeof attributes.path === "undefined") {
        attributes.path = [
          "M",
          this.start.x,
          this.start.y,
          "L",
          this.end.x,
          this.end.y
        ].join(" ");
      }
    }

    super.repaint(attributes);
  }
}

namespace.shape.Line = Line;
